package com.homework;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class ServletContextDemo extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取ServletContext对象
        ServletContext servletContext = getServletContext();

        //向域对象添加数据
        servletContext.setAttribute("username","nigulasi");
        //获取名称
        String servletContextName = servletContext.getServletContextName();
        System.out.println(servletContextName);

        //获取访问虚拟路径
        String contextPath = servletContext.getContextPath();
        System.out.println(contextPath);
        //获取a的路径
        String realPath1 = servletContext.getRealPath("/WEB-INF/classes/a.txt");
        System.out.println(realPath1);
        //获取b的路径
        String realPath2 = servletContext.getRealPath("/b.txt");
        System.out.println(realPath2);
        //获取c的路径
        String realPath3 = servletContext.getRealPath("/WEB-INF/c.txt");
        System.out.println(realPath3);

        //访问虚拟目录获取应用的磁盘绝对路径
        String realPath = servletContext.getRealPath("/");
        System.out.println(realPath);
        //获取服务器名称和版本
        String serverInfo = servletContext.getServerInfo();
        System.out.println(serverInfo);

        //
        String value = servletContext.getInitParameter("xiaowang");
        System.out.println(value);


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
